wxapp.logic.socket = {
    CONNECT_STATUS_OK: 1,
    CONNECT_STATUS_OPENING: 2,
    CONNECT_STATUS_CLOSING: 3,
    CONNECT_STATUS_CLOSE: 4,
    CONNECT_STATUS_ERROR: 0,
    _send_count: 0,
    _receive_count: 0,
    _socketMsgQueue: [],
    connectStatus: 0,
    filterCmds: wxapp.appData.netMsgFilter,
    _addSocketMsgQueue: function(t) {
        t.cmd === tywx.EventType.CMD_BIND_USER && this._socketMsgQueue.push(t);
    },
    connect: function(t) {
        var e = this;
        if (e.connectStatus != e.CONNECT_STATUS_OPENING && e.connectStatus != e.CONNECT_STATUS_OK) {
            e.connectStatus = e.CONNECT_STATUS_OPENING, wxapp.tools.bilog.clickStat(tywx.clickStatEventType.clickStatEventTypeTCPStart, [ t ]);
            try {
                wx.connectSocket({
                    url: t
                });
            } catch (t) {
                e.connectStatus = e.CONNECT_STATUS_ERROR, tywx.NotificationCenter.trigger(tywx.EventType.TCP_ERROR);
            }
            wx.onSocketOpen(function(n) {
                e.connectStatus = e.CONNECT_STATUS_OK;
                for (var c = 0, o = e._socketMsgQueue.length; c < o; c++) e.sendMsg(e._socketMsgQueue[c]);
                e._socketMsgQueue = [], wxapp.tools.bilog.clickStat(tywx.clickStatEventType.clickStatEventTypeTCPSuccess, [ t ]), 
                tywx.NotificationCenter.trigger(tywx.EventType.TCP_OPENED);
            }), wx.onSocketError(function(n) {
                wxapp.tools.bilog.clickStat(tywx.clickStatEventType.clickStatEventTypeTCPFailed, [ t ]), 
                e.connectStatus = e.CONNECT_STATUS_ERROR, tywx.NotificationCenter.trigger(tywx.EventType.TCP_ERROR);
            }), wx.onSocketClose(function(t) {
                e.connectStatus = e.CONNECT_STATUS_CLOSE, tywx.NotificationCenter.trigger(tywx.EventType.TCP_CLOSE);
            }), wx.onSocketMessage(function(t) {
                var n = e.decodeMessage(t.data);
                if (null != n && "0000" != n) {
                    var c = n.substr(0, n.length - 0);
                    if (null != c && c.length > 0) {
                        var o = JSON.parse(c);
                        -1 == e.filterCmds.indexOf(o.cmd) && (e._receive_count++, o.cmd, wxapp.saveNetProto && wxapp.tools.debug.addNetProt(o)), 
                        tywx.NotificationCenter.trigger(tywx.EventType.TCP_RECEIVE, o);
                    }
                }
            });
        }
    },
    reconnet: function() {
        var t = this;
        tywx.StateInfo.isOnForeground && (t.connectStatus != t.CONNECT_STATUS_ERROR && t.connectStatus != t.CONNECT_STATUS_CLOSE || (tywx.NotificationCenter.trigger(tywx.EventType.TCP_RECONNECT), 
        t.connect(tywx.SystemInfo.webSocketUrl)));
    },
    close: function() {
        var t = this;
        t.connectStatus = t.CONNECT_STATUS_CLOSING, wx.closeSocket();
    },
    sendMsg: function(t) {
        var e = this;
        if (e.connectStatus == e.CONNECT_STATUS_OK) {
            var n = JSON.stringify(t);
            e.filterCmds.indexOf(t.cmd), wx.sendSocketMessage({
                data: n,
                success: function(n) {
                    t.cmd == tywx.EventType.CMD_HEART_BEAT || (e._send_count++, e.filterCmds.indexOf(t.cmd), 
                    wxapp.saveNetProto && wxapp.tools.debug.addNetProt(t));
                },
                fail: function(n) {
                    var c = arguments[0];
                    c && "sendSocketMessage:fail taskID not exist" === c.errMsg && (wx.closeSocket(), 
                    e.connectStatus = e.CONNECT_STATUS_ERROR), e._addSocketMsgQueue(t), tywx.NotificationCenter.trigger(tywx.EventType.TCP_ERROR), 
                    tywx.NotificationCenter.trigger(tywx.EventType.TCP_SEND_ERROR, t);
                },
                complete: function(t) {}
            });
        } else e._addSocketMsgQueue(t);
    },
    decodeMessage: function(t) {
        if ("undefined" != typeof ArrayBuffer && t instanceof ArrayBuffer) {
            for (var e = new Uint8Array(t), n = "", c = 0, o = e.length; c < o; c++) n += String.fromCharCode(e[c]);
            return n;
        }
        for (var i = (t = tywx.EncodeDecode.base64Decode(t)).slice(0, 4), c = 0, o = (t = t.slice(4)).length; c < o; c++) {
            var s = t[c];
            s ^= i[c % 4], t[c] = s;
        }
        return tywx.EncodeDecode.utf8Decode(t);
    }
};